import {
  httpTokenRequest
} from '@/utils/http.js';
export default {
  data() {
    return {
      menuList: [{
        icon: '/static/img/wenTiKu/wx.png',
        name: '微信好友',
        remark: '',
        url: '',
        handler: 'shareTo'
      },
      {
        icon: '/static/img/wenTiKu/pyq.png',
        name: '朋友圈',
        remark: '',
        url: '',
        handler: 'shareToMoments'
      },
      // {
      //   icon: '/static/img/wenTiKu/qq.png',
      //   name: 'QQ好友',
      //   remark: '',
      //   url: '',
      //   handler: 'toShareQq'
      // },
      // {
      //   icon: '/static/img/wenTiKu/copyLink.png',
      //   name: '复制链接',
      //   remark: '',
      //   url: '',
      //   handler: 'copyLink'
      // },
      {
        icon: '/static/img/wenTiKu/collect_0.png',
        name: '收藏',
        remark: '',
        url: '',
        handler: 'collect'
      }, {
        icon: '/static/img/wenTiKu/fontSize.png',
        name: '阅读字号',
        remark: '',
        url: '',
        handler: 'changShowFontSize'
      }],
      details: null,
      sourceList: [],
      showMore: false,
      shareId: null,
      showFontSize: false,
      fontSizeIndex: 0,
      fontSize: '28rpx',
      showParse: true,
    };
  },
  methods: {
    //获取详细信息
    getInfo(id) {
      uni.showToast({
        title: '加载中',
        icon: 'loading'
      });
      httpTokenRequest({
        url: this.$paths.getInfo,
        method: 'POST',
        params: {
          id: id
        }
      })
        .then((res) => {
          if (res.code == 1) {
            uni.hideLoading()
            this.details = res.data;
            this.menuList[4].icon = this.details.is_collect == 1 ? '/static/img/wenTiKu/collect_1.png' : '/static/img/wenTiKu/collect_0.png';
            this.menuList[4].name = this.details.is_collect == 1 ? '已收藏' : '收藏';
          } else {
            this.$tools.showToast(res.msg);
          }
        })
        .catch((err) => {
          console.log('错误回调', err);
        });
    },

    //关闭弹框
    close() {
      this.showMore = false;
    },

    //点击更多中的菜单
    menuClick(item) {
      if (item.handler) {
        this[item.handler]();
      } else if (item.url) {
        this.$used.href(item.url);
      }
    },

    //微信好友
    shareTo() {
      var that = this
      let code = uni.getStorageSync('userInfo').invite_code;
      let obj = {
        id: 'gh_d8bf9808c336',
        path: '/pages2/zhunzeInfo/zhunzeInfo?id=' + that.shareId + '&code=' + code,
        webUrl: that.$h5Url.server + '/pages/downLoad/downLoad'
      };
      console.log(obj)
      uni.share({
        provider: 'weixin',
        scene: 'WXSceneSession',
        type: 5,
        miniProgram: obj,
        imageUrl: '/static/logo-min.png',
        title: that.details.name,
        success: function (res) {
          console.log('success:' + JSON.stringify(res));
          that.showMore = false;
        },
        fail: function (err) {
          console.log('fail:' + JSON.stringify(err));
        }
      });
    },

    //分享朋友圈
    shareToMoments() {
      var that = this
      var share_url = this.$h5Url.server + '/pages/details/zhunzeInfo?id=' + this.shareId;
      uni.share({
        provider: 'weixin',
        scene: 'WXSceneTimeline',
        type: 0,
        href: share_url,
        title: that.details.name,
        summary: that.removeTextTag().substring(0, 100),
        imageUrl: '/static/logo-min.png',
        success: function (res) {
          console.log('success:' + JSON.stringify(res));

        },
        fail: function (err) {
          console.log('fail:' + JSON.stringify(err));
        }
      });
    },

    //分享QQ
    toShareQq() {
      var that = this
      var share_url = this.$h5Url.server + '/pages/details/zhunzeInfo?id=' + this.shareId;
      uni.share({
        provider: 'qq',
        scene: 'WXSceneSession',
        type: 0,
        href: share_url,
        title: that.details.name,
        summary: that.removeTextTag().substring(0, 100),
        imageUrl: '/static/logo-min.png',
        success: function (res) {
          console.log('success:' + JSON.stringify(res));
          that.showMore = false;
        },
        fail: function (err) {
          console.log('fail:' + JSON.stringify(err));
        }
      });
    },


    //复制链接
    copyLink() {
      var that = this;
      uni.setClipboardData({
        data: that.$h5Url.server + '/pages/details/zhunzeInfo?id=' + that.shareId,
        success: function () {
          uni.showToast({
            title: '链接复制成功'
          });
          that.showMore = false;
          uni.showToast({
            title: '链接复制成功'
          });
        }
      });
    },


    //收藏 or 取消收藏
    collect() {
      httpTokenRequest({
        method: 'POST',
        url: this.$paths.accountantCollect,
        params: {
          id: this.details.id
        }
      })
        .then((res) => {
          if (res.code == 1) {
            //收藏和未收藏切换
            this.details.is_collect = 1 - this.details.is_collect;
            console.log(this.details.is_collect);
            this.menuList[4].icon = `/static/img/wenTiKu/collect_${this.details.is_collect}.png`;
            if (this.details.is_collect == 1) {
              this.$tools.showToast('收藏成功');
              this.menuList[4].icon = `/static/img/wenTiKu/collect_1.png`;
              this.menuList[4].name = '取消收藏';
            } else if (this.details.is_collect == 0) {
              this.menuList[4].icon = `/static/img/wenTiKu/collect_0.png`;
              this.$tools.showToast('取消收藏');
              this.menuList[4].name = '收藏';
            }
            setTimeout(() => {
              this.showMore = false;
            }, 1000);
          } else {
            this.$tools.showToast(res.msg);
          }
        })
        .catch((err) => {
          console.log('错误回调', err);
        });
    },

    // 切换字体弹出
    changShowFontSize() {
      this.showFontSize = !this.showFontSize
      this.showMore = false
    },

    // 切换字体
    changFontSize(index) {
      this.fontSizeIndex = index
      switch (index) {
        case 0:
          this.fontSize = '28rpx'
          break
        case 1:
          this.fontSize = '36rpx'
          break
        case 2:
          this.fontSize = '46rpx'
          break
      }
      this.showParse = false
      this.$nextTick(() => {
        this.showParse = true
      })
    },


    //格式化富文本内容
    removeTextTag(str) {
      return str = this.details.info_content.replace(/<[^<>]+>/g, "").replace(/&nbsp;/gi, "");
    },
  },
  computed: {
    'fontSizeLeft': function () {
      return `primary${this.fontSizeIndex + 1}`
    }
  },
  onLoad(e) {
    this.getInfo(e.id);
    this.shareId = e.id;
  },
}